package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
)

//https://blog.csdn.net/weixin_62700590/article/details/137536462
//https://blog.csdn.net/book_longker/article/details/123760632

func main() {
	server := gin.Default()
	server.LoadHTMLGlob("templates/*")
	server.Static("/static", "./static")
	server.GET("/mortal", func(a *gin.Context) {
		a.JSON(200, gin.H{
			"code": 200,
			"data": "mortal",
		})
	})
	server.GET("/index", func(context *gin.Context) {
		context.HTML(200, "index.html", gin.H{
			"msg": "这是后端传来的数据",
		})
	})

	// 传统传参方法: user/info?userid=1&username=mortal
	server.GET("/user/info", func(context *gin.Context) {
		userid := context.Query("userid")
		username := context.Query("username")
		context.JSON(http.StatusOK, gin.H{
			"userid":   userid,
			"username": username,
		})
	})

	// restful传参方法: user/data/1/mortal
	server.GET("/user/data/:userid/:username", func(context *gin.Context) {
		userid := context.Param("userid")
		username := context.Param("username")
		context.JSON(http.StatusOK, gin.H{
			"userid":   userid,
			"username": username,
		})
	})
	//get传值绑定结构体
	type Userinfo struct {
		Username string `form:"username" json:"user"`
		Password string `form:"password" json:"password"`
	}
	server.GET("/", func(context *gin.Context) {
		var userinfo Userinfo
		if err := context.ShouldBind(&userinfo); err == nil {
			context.JSON(http.StatusOK, userinfo)
		} else {
			context.JSON(http.StatusBadRequest, gin.H{
				"error": err.Error(),
			})
		}
	})

	//post 接收raw数据
	server.POST("/json", func(context *gin.Context) {
		b, _ := context.GetRawData()
		var m map[string]interface{}
		_ = json.Unmarshal(b, &m)
		context.JSON(http.StatusOK, m)
	})
	//post 接收表单数据
	server.POST("/add", func(context *gin.Context) {
		username := context.PostForm("username")
		password := context.PostForm("password")
		context.JSON(http.StatusOK, gin.H{
			"code":     200,
			"msg":      "ok",
			"username": username,
			"password": password,
		})
	})
	//post传值到结构体
	server.POST("/dologin", func(context *gin.Context) {
		var userinfo Userinfo
		if err := context.ShouldBind(&userinfo); err == nil {
			context.JSON(http.StatusOK, userinfo)
		} else {
			context.JSON(http.StatusBadRequest, gin.H{
				"error": err.Error(),
			})
		}
	})
	//获取post xml数据
	type Article struct {
		Title   string `json:"title" xml:"title"`
		Content string `json:"content" xml:"content"`
	}
	//	<?xml version="1.0" encoding="UTF-8"?>
	//	<article>
	//	<content type="string">我是张三</content>
	//	<title type="string">张三</title>
	//	</article>
	server.POST("/xml", func(context *gin.Context) {
		var article Article
		if err := context.ShouldBindXML(&article); err == nil {
			context.JSON(http.StatusOK, article)
		} else {
			context.JSON(http.StatusBadRequest, gin.H{
				"err": err.Error(),
			})
		}

	})
	//重定向
	server.GET("/redirect", func(ctx *gin.Context) {
		ctx.Redirect(http.StatusMovedPermanently, "http://www.baidu.com")
	})
	//404 no route
	server.NoRoute(func(ctx *gin.Context) {
		ctx.HTML(http.StatusNotFound, "404.html", nil)
	})
	// 路由组
	userGroup := server.Group("/user")
	{
		userGroup.GET("list", func(context *gin.Context) {
			context.JSON(http.StatusOK, gin.H{
				"msg": "获取用户列表",
			})
			userGroup.POST("/infos", func(context *gin.Context) {
				context.JSON(http.StatusOK, gin.H{
					"msg": "获取用户信息",
				})
			})
		})
	}

	server.Run(":9090")
}
